import Vue from 'vue'
import Router from 'vue-router'
import store from '../vuex/index'

Vue.use(Router)
//布局页路由
const layout = [
    {
        path: '/index111',
        component: resolve => require(['../views/index.vue'], resolve),
        meta: { requireAuth: false }
    },
]
//主路由
const routes = [
    {
        path: '/',
        component: resolve => require(['../views/yh.vue'], resolve)
    },
    //带底部主页
    {
        path: '/layout',
        component: resolve => require(['../views/layout/layout.vue'], resolve),
        children: layout
    },
    {
        path: '/index',
        component: resolve => require(['../views/index.vue'], resolve),
        meta: { requireAuth: false }
    },
    {
        path: '/listMb',
        component: resolve => require(['../views/listMb.vue'], resolve),
        meta: { requireAuth: false }
    },
    {
        path: '*',
        component: resolve => require(['../views/NotFind.vue'], resolve),
    },//全不匹配的情况下，返回404，路由按顺序从上到下，依次匹配。最后一个*能匹配全部，
]
console.log(routes);
// 页面刷新时，重新赋值token
if (window.localStorage.getItem('tokenId')) {
    // store.state.tokenId = window.localStorage.getItem('tokenId');
    // store.state.userInfo = JSON.parse(window.localStorage.getItem('userInfo'));
    // store.state.userId = window.localStorage.getItem('userId');
    // sessionStorage.removeItem("selectRedP");


    //store.state.common=JSON.parse(hgetStore("common"));
}
/*页面刷新重新赋值commmon*/
//给公共参数重新赋值
if (hgetStore("hvuex")) {
    console.log(JSON.parse(hgetStore("hvuex")));
    store.dispatch("hvuex", JSON.parse(hgetStore("hvuex")));
}
//方便修改
function hvuex(obj){
    store.dispatch("hvuex",obj);
}
window.hvuex=hvuex;

const router = new Router({
    hashbang: true,
    history: true,
    transitionOnLoad: true,
    routes
});

//拦截
router.beforeEach((to, from, next) => {
    // console.log(to);
    // console.log(from);
    // console.log(next);
    next();
    //return; //判断是否需要登录
    // if (to.matched.some(record => record.meta.requireAuth)) {
    //     //判断Token
    //     if (store.state.token) {
    //         //需要引入store
    //         next();
    //     } else {
    //         //Token失效
    //         next({ path: '/login' });
    //     }
    // } else {
    //     next();
    // }
});
export default router;